<?php
include_once "MPDF54/mpdf.php";


class Lib_Pdf {
    private $CI;
    
    public function __construct() {
        $this->CI =& get_instance();
    }
    
    // vrati zadanie ako pdf
    public function getTask($id, $dest="") {
        // ziska si task
        $task = $this->CI->tasks_model->getByIdJoined($id);
        // vyrobi pdf
        
       
        
        //var_dump($table);exit;
        $mpdf=new mPDF();
        $mpdf->WriteHTML($this->getTaskHtml($task));
        $mpdf->Output($dest);
    }
    
    private function getTaskHtml($task) {
         $header = "<div>
                <h3>$task->name</h3>
                <h4>$task->book_name (strana $task->page)</h4>
                </div>
                <hr/>";
        
        $body = "<strong>Popis:</strong><br/>$task->description";
        $body .= "<h4> Hodnoty </h4> ";
        $table = '<table border="1" style="width: 100%">';
        
        for ($i=0;$i<$task->functions_count+1;$i++) {
            $table .= "<tr>";
            if ($i==0) {
                $table .= "<td style='width: 70px;'>x [$task->x_units]</td>";
            } else {
                $table .= "<td style='width: 70px;'>f$i(x) [$task->y_units]</td>";
            }
            
            for ($j=0;$j<10;$j++) {
               $table .= "<td>"."</td>";
            }
            $table .="</tr>";
        }
        $table .= '</table>';
        
        return $header.$body.$table;
    }
    
    // vrati vypracovane zadanie ako pdf
    public function getSolution($fk_tasks, $data) {
        // task
        $task = $this->CI->tasks_model->getByIdJoined($fk_tasks);
        
           $header = "<div>
                <h3>$task->name</h3>
                <h4>$task->book_name (strana $task->page)</h4>
                </div>
                <hr/>";
        
        $body = "<strong>Popis:</strong><br/>$task->description";
        $body .= "<h4> Hodnoty </h4> ";
        $table = '<table border="1" style="width: 100%">';
        
        for ($i=0;$i<$task->functions_count+1;$i++) {
            $table .= "<tr>";
            if ($i==0) {
                $table .= "<td style='width: 70px;'>x [$task->x_units]</td>";
            } else {
                $table .= "<td style='width: 70px;'>f$i(x) [$task->y_units]</td>";
            }
            
            $dataArr = json_decode($data);
            for ($j=0;$j<10;$j++) {
               $table .= "<td>".$dataArr[$j][$i]."</td>";
            }
            $table .="</tr>";
        }
        $table .= '</table>';
        
        $encodedData = urlencode(urlencode($data));
        $graph = "<img src='".  base_url()."/tasks/toGraph/$fk_tasks/$encodedData' />";
        
        $mpdf=new mPDF();
        $mpdf->debug=true;
        $mpdf->WriteHTML($header.$body.$table.$graph);
        $mpdf->Output();
        
        // vyrobi pdf
       
    }
  
}

?>
